import Taro, { useState, useEffect, useRouter } from '@tarojs/taro'
import { View, Text, Block } from '@tarojs/components'
import { IIndex } from '../../interfaces/index'
import { IError } from '../../interfaces/error'
import useBasePage from '../basePage/useBasePage'
import { observer } from '@tarojs/mobx'
import { goToErrorPage, redirectToPage } from '../../utils/index'

const Error = () => {
  const { params } = useRouter()
  const { onload, awtRs } = useBasePage()
  const [ pageTitle, setPageTitle ] = useState<IIndex['pageTitle']>('访问异常')
  const [ init, setInit ] = useState<IIndex['init']>(false)
  const [ pageInfo, setPageInfo ] = useState<IError['pageInfo']>({ msg: '' })

  useEffect(() => {
    onload(async () => {
      console.log('参数：', params)
      switch (params.type) {
        case '400':
          setPageInfo({ msg:'请求参数有误' })
          break
        case '404':
          setPageInfo({ msg:'请求的资源不存在' })
          break
        case '405':
          setPageInfo({ msg:'没有权限进入该页面' })
          break
        case '500':
          setPageInfo({ msg:'系统开⼩差' })
          break
        case '501':
          setPageInfo({ msg:'合法域名未配置' })
          break
        case '504':
          setPageInfo({ msg:'网络不给力，请稍后再试' })
          break
        case '505':
          setPageInfo({ msg:'程序配置异常，加载失败' })
          break
        default:
          setPageInfo({ msg: '未知错误，状态码：' + params.type })
      }
      if (params.msg) setPageInfo({ msg: params.msg })
      setInit(true)
    }, pageTitle)
  }, [])
  // 返回首页
  const goBackIndex = () => {
    redirectToPage('/pages/home/index')
  }

  return (
    <View className='page'>
      {init &&
        <Block>
          <Text>{pageInfo.msg}</Text>
          <View onClick={goBackIndex}>返回首页</View>
          <View onClick={() => goToErrorPage(404, '122')}>模拟报错</View>
        </Block>
      }
    </View>
  )
}

export default observer(Error)
